<?php
/**
 * 标签名称，比如这个使用!{widget }这样调用
 * @var string
 */
$self_tag_name = 'widget';

/**
 * 执行某个组件，比如!{widget id='123456'}
 * @param array $params 参数
 * @param Smarty $smarty
 */
function smarty_function_widget($params, &$smarty){
	if(!isset($params['id'])){
		return '';
	}
	$id = trim($params['id']);
	global $page_tree;
	if($page_tree === null){
		if(defined('TPL_VERSION')){
			// 如果指定了某个未发布版本
			$page_codition = array('site_id' => SITE_ID, '_id' => TPL_VERSION);
		}else{
			// TEMPLATE_NAME //模板名称
			$page_codition = array('site_id' => SITE_ID, 'name' => TEMPLATE_NAME);
		}
		$page_mod = new PageModel();
		$page_conf = $page_mod->get_by_attrs($page_codition);
		$page_tree = new PageTree($page_conf, $page_mod);
	}
	
	//获取配置文件中的上组件的配置
	$widget_conf = $page_tree->get_item($id);
	if(empty($widget_conf)){
		return '组件未找到：id=' . $id;
	}
	return widget_display ($widget_conf, $smarty);
}


/**
 * 获取要显示的组件HTML
 * @param widget_conf 组件节点配置
 * @param smarty Smarty实例
 */

function widget_display($widget_node, $smarty=null) {
	if($smarty == null){
		$smarty = Viewer::get_view();
	}
	$id = $widget_node['id'];
	$smarty->assign2($widget_node);
	
	$name = $widget_node['name'];
	$type = $widget_node['type'];
	$cate = $widget_node['cate'];
	$tpl = $widget_node['template'];
	
	//获取使用的模板
	$widget_path = ROOT_PATH . '/protected/includes/widgets/';
	//首先从用户目录中寻找
	$template_path = USER_TEMPLATE_PATH . 'data/widget_template/' . $cate . '/' . $name . '/template/' . $tpl . '.' . TEMPLATE_TYPE;
	if(!is_file($template_path)){
		//尝试公共目录中寻找
		$template_path = $widget_path . $cate . '/' . $name . '/template/' . $tpl . '.' . TEMPLATE_TYPE;
		if(!is_file($template_path)){
			if(defined('YU_DEBUG') && YU_DEBUG === true){
				return '组件模板不存在2：{id:' . $id . ',type:' . $type . ',name:' . $name . ',template:' . $tpl .',cate:' . $cate . '};' . $template_path;
			}
			return '';
		}
	}
	
	//组件类
	$widget_class_name = ucfirst($cate) . ucfirst($name) . 'Widget';
	$widget_class_path = $widget_path . $cate . '/' . $name . '/' . $widget_class_name . '.php';
	if(is_file($widget_class_path)){
		include_once $widget_class_path;
		$widget = new $widget_class_name;
		if(isset($widget_node['config'])){
			$widget->init($widget_node['config']);
			$smarty->assign2('config', $widget_node['config']);
		}else{
			$c = $widget->default_val();
			if(!empty($c)){
				$smarty->assign2('config', $c);
			}
		}
		$data = $widget->get_data();
		$smarty->assign2($data);
	}else{
		if(defined('YU_DEBUG') && YU_DEBUG === true){
			return '组件模板不存在1：{id:' . $id . ',type:' . $type . ',name:' . $name . ',template:' . $tpl .',cate:' . $cate . '};' . $widget_class_path;
		}
		return '';
	}
	//展示
	$template_content = file_get_contents($template_path);
	return $smarty->compile($template_content);
}

/**
 * 获取组件内容（编译后）
 * @param string $wiget_cate 组件分类
 * @param string $widget_name 组件名称
 * @param string $widget_tpl 组件模板
 * @param array $config
 */
function get_widget_content($node){
	$wiget_cate = $node['cate'];
	$widget_name = $node['name'];
	$widget_tpl = $node['template'];
	$id = $node['id'];
	
	$widget_path = USER_TEMPLATE_PATH . 'data/widget_template/' . $wiget_cate . '/' . $widget_name . '/';
	$template_path = $widget_path . 'template/' . $widget_tpl . '.' . TEMPLATE_TYPE;
	if(!is_file($template_path)){
		//尝试公共目录中寻找
		$widget_path = ROOT_PATH . '/protected/includes/widgets/' . $wiget_cate . '/' . $widget_name . '/' ;//首先从用户目录中寻找
		$template_path = $widget_path . 'template/' . $widget_tpl . '.' . TEMPLATE_TYPE;
		if(!is_file($template_path)){
			if(defined('YU_DEBUG') && YU_DEBUG === true){
				return '组件模板不存在1：{cate:' . $wiget_cate . ',name:' . $widget_name . ',template:' . $widget_tpl .'};';
			}
			return '';
		}
	}
	//组件类
	$widget_class_name = ucfirst($widget_name) . 'Widget';
	$widget_class_path = $widget_path . $widget_class_name . '.php';
	$smarty = null;
	if(is_file($widget_class_path)){
		include_once $widget_class_path;
		$widget = new $widget_class_name;
		$smarty = Viewer::get_view();
		$smarty->left_delimiter = '#{';
		if(isset($node['config'])){
			$config = $node['config'];
		}else{
			$config = $widget->default_val();
		}
		if(!empty($config)){
			$smarty->assign2('config', $config);
		}
		$smarty->assign2($node);
		$widget->init($config);
		$data = $widget->get_data();
// 		if($widget_name === 'recommend'){
// 			print_r($data);exit;
// 		}
		$smarty->assign2($data);
	}else{
		if(defined('YU_DEBUG') && YU_DEBUG === true){
			return '组件类不存在：{cate:' . $wiget_cate . ',name:' . $widget_name . ',template:' . $widget_tpl .',path='.$widget_class_path.'};';
		}
		return '';
	}
	//展示
	$template_content = file_get_contents($template_path);
	return $smarty->compile($template_content);
}